package com.nageoffer.shortlink.admin.romote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.admin.common.result.Result;
import com.nageoffer.shortlink.admin.romote.dto.req.ShortLinkCreateReqDto;
import com.nageoffer.shortlink.admin.romote.dto.req.ShortLinkPageReqDto;
import com.nageoffer.shortlink.admin.romote.dto.req.ShortLinkUpdateReqDto;
import com.nageoffer.shortlink.admin.romote.dto.resp.ShortLinkCountQueryRespDto;
import com.nageoffer.shortlink.admin.romote.dto.resp.ShortLinkCreateRespDto;
import com.nageoffer.shortlink.admin.romote.dto.resp.ShortLinkPageRespDto;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 */
public interface ShortLinkRemoteService {


    default Result<ShortLinkCreateRespDto> createShortLink(ShortLinkCreateReqDto requestParam){
        String resultBody = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {
        });
    }


     default Result<IPage<ShortLinkPageRespDto>> pageShortLink(ShortLinkPageReqDto requestParam){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default Result<List<ShortLinkCountQueryRespDto>> listGroupShortLinkCount(List<String> requestParam){
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default void updateShortLink(ShortLinkUpdateReqDto requestParam){
        String resultBody = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/update", JSON.toJSONString(requestParam));
    }

    default Result<String > getTitleByUrl(@RequestParam("url") String url){
        String resultStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url="+url);
        return JSON.parseObject(resultStr, new TypeReference<>() {
        });
    }
}
